package kr.ac.jejunu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import kr.ac.jejunu.model.Comment;
import kr.ac.jejunu.service.CommentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/main")
public class CommentController {
	
	@Autowired
	private CommentService commentService;
	
	@RequestMapping
	public String listView(Model model, HttpServletRequest request) {
		List<Comment> data = commentService.getList();
		model.addAllAttributes(data);
		request.setAttribute("comments", data);
		return "main";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String save(@ModelAttribute("comment") Comment comment){
		commentService.saveComment(comment);
		return "redirect:/main";
	}
	
//	@RequestMapping(method=RequestMethod.GET)
//	public String showForm(){
//		return "main";
//	}
}
